<template>
  <div>
    <van-pull-refresh v-model="isLoading" @refresh="onRefresh">
    <van-list
      v-model="loading"
      :finished="finished"
      finished-text="没有更多了"
      @load="onLoad"
      :immediate-check='false'
    >
      <AriticleItem
        v-for="obj in list"
        :key="obj.art_id"
        :artObj="obj"
        @dislike = dislikeFn
        @reportEV = reportFn
        @click.native="itemClickFn(obj.art_id)"
      ></AriticleItem>
    </van-list>
  </van-pull-refresh>
  </div>
</template>

<script>
import { getAllAriticleListAPI, dislikeArticleAPI, articleReportsAPI } from '@/api'
import AriticleItem from './AriticleItem.vue'
import { Notify } from 'vant'
export default {
  props: ['channelId'],
  data () {
    return {
      list: [],
      loading: false,
      finished: false,
      theTime: new Date().getTime(),
      isLoading: false
    }
  },
  components: {
    AriticleItem
  },
  async created () {
    this.update()
  },
  methods: {
    async update () {
      const res2 = await getAllAriticleListAPI({
        channel_id: this.channelId,
        timestamp: this.theTime
      })
      this.list = [...this.list, ...res2.data.data.results]
      this.theTime = res2.data.data.pre_timestamp

      if (res2.data.data.pre_timestamp === null) {
        this.finished = true
      }

      // 加载状态结束
      this.loading = false
      this.isLoading = false
    },
    async onLoad () {
      this.update()
    },
    async onRefresh () {
      this.list = []
      this.theTime = new Date().getTime()
      this.update()
    },
    async dislikeFn (id) {
      try {
        await dislikeArticleAPI({ artId: id })
        Notify({ type: 'success', message: '反馈成功' })
      } catch (error) {
        Notify({ type: 'warning', message: '反馈失败-联系程序员' })
      }
    },
    async reportFn (id, type) {
      console.log('aaaa' + id, 'aaaa' + type)
      try {
        await articleReportsAPI({ artId: id, type: type })
        Notify({ type: 'success', message: '反馈成功' })
      } catch (error) {
        console.log(error)
      }
    },
    itemClickFn (id) {
      this.$router.push({
        path: `/detail?art_id=${id}`
      })
    }
  }
}
</script>

<style>
</style>
